package com.leyou.item.controller;


import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("spec")
public class SpecController {

    private SpecGroupService groupService;
    private SpecParamService paramService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }

    /**
     * 根据商品分类id查询规格组
     * @param categoryId  商品分类id
     * @return 返回规格组集合
     */
     @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategoryId(@RequestParam("id") Long categoryId){
        /* List<SpecGroup> specGroups = groupService.query().eq("category_id", id).list();
         List<SpecGroupDTO> groupDTOS = SpecGroupDTO.convertEntityList(specGroups);
         return  ResponseEntity.ok(groupDTOS);*/
      List<SpecGroupDTO> list=groupService.queryGroupByCategoryId(categoryId);
       return ResponseEntity.ok(list);
     }

    /**
     * 查询规格参数集合
     * @param categoryId 分类id
     * @param groupId  规格组id
     * @param searching 是否搜索
     * @return 参数集合
     */
     @GetMapping("params")
    public  ResponseEntity<List<SpecParamDTO>> querySpecParams(
             @RequestParam(value = "categoryId", required = false) Long categoryId,
             @RequestParam(value = "groupId", required = false) Long groupId,
             @RequestParam(value = "searching", required = false) Boolean searching
     ){
         return ResponseEntity.ok(paramService.queryParams(categoryId, groupId, searching));
     }

    /**
     * 新增规格组
     * @param specGroupDTO 规格组信息
     * @return
     */
     @PostMapping("group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO specGroupDTO){
        //新增规格组
         groupService.save(specGroupDTO.toEntity(SpecGroup.class));
          //返回结果
         return ResponseEntity.status(HttpStatus.CREATED).build();
     }
    /**
     * 修改规格组
     * @param groupDTO 规格组信息
     * @return
     */
    @PutMapping("group")
    public ResponseEntity<Void> updateGroup(
            @RequestBody SpecGroupDTO groupDTO){
        // 更新规格组
        groupService.updateById(groupDTO.toEntity(SpecGroup.class));
        // 返回结果
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规划参数
     * @param specParamDTO 规格组信息
     * @return
     */
    @PostMapping("/param")
   public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO specParamDTO){
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        paramService.save(specParam);
       return  ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParamDTO 规格组信息
     * @return 没有返回值
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO specParamDTO){
     paramService.updateById(specParamDTO.toEntity(SpecParam.class));
     return  ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param categoryId
     * @return
     */
     @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long categoryId){
        return ResponseEntity.ok(groupService.querySpecList(categoryId));
     }

}
